home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Selection / Multimedia Selection Volume One - CD-ROM / MULTIMEDIA SELECTION____________.ISO / programz / ldb / binder.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  8.4 KB  |  303 lines

  1. /*
  2.  
  3.     binder.hpp
  4.     10-25-91
  5.     Loose Data Binder v 1.5
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.  
  11.     PSW / Power SoftWare
  12.     P.O. Box 10072
  13.     McLean, Virginia 22102 8072 USA
  14.  
  15.     John Small
  16.     Voice: (703) 759-3838
  17.     CIS: 73757,2233
  18.  
  19. */
  20.  
  21.  
  22. #ifndef BINDER_HPP
  23. #define BINDER_HPP
  24.  
  25. #include <limits.h>    // UINT_MAX
  26.  
  27. /*
  28.  
  29.     The "Loose Data Binder", or Binder for short, binds
  30.     any type of data together in a hybrid stack-queue-
  31.     deque-list-array structure.  Like a loose leaf
  32.     notebook where you can insert or arrange leafs in
  33.     any fashion, the "Loose Data Binder" allows you to
  34.     collect and arrange data in any fashion.  Think of
  35.     the Binder as an elastic array of void pointers.
  36.     The Binder is the equivalent of an extensive 
  37.     container class hierarchy but without the confusing 
  38.     complexity of a towering hierarchy.
  39.  
  40. */
  41.  
  42.  
  43. /*  Various pointers and their NULLs  */
  44.  
  45. typedef void * voiD;
  46. #define voiD0  ((voiD)0)
  47. typedef voiD * voiDV;
  48. #define voiDV0 ((voiDV)0)
  49.  
  50.  
  51. enum BDR_DEFAULTS  {
  52.  
  53.     BDR_MAXNODES    =    (UINT_MAX/sizeof(voiD)),
  54.     BDR_LIMIT    =    20,
  55.     BDR_DELTA    =    10,
  56.     BDR_NOTFOUND    =    BDR_MAXNODES
  57. };
  58.  
  59. enum BDR_FLAG_BITS  {
  60.  
  61.     BDR_SORTED    =    0x01,
  62.     BDR_OK_FREE    =    0x02,
  63.     BDR_NO_FREE    =    0x00
  64. };
  65.  
  66.  
  67. /*  Search/sort compare function pointer type:  */
  68.  
  69. typedef int (*BDRcomparE)(const voiD D1, const voiD D2);
  70. #define BDRcomparE0 ((BDRcomparE)0)
  71.  
  72.  
  73. /*  Function pointer types for the Binder iterators:  */
  74.  
  75. typedef void (*BDRforEachBlocK)
  76.         (voiD D, voiD M, voiD A);
  77. typedef int (*BDRdetectBlocK)
  78.         (voiD D, voiD M);
  79. class Binder;
  80. typedef Binder * BindeR;
  81. typedef void (*BDRcollectBlocK)
  82.         (voiD D, BindeR R, voiD M, voiD A);
  83.  
  84. /*
  85.     Gimmick for constructing without initializing: 
  86.     to be used for loading instances from a stream 
  87.     (see sbinder.hpp).
  88. */
  89.  
  90. typedef BindeR * UniqueBinderConST;
  91. #define OnlyInitBinderVFT ((UniqueBinderConST)0)
  92.  
  93.  
  94. /*  
  95.     Gaurrantees Dfree() is only invoked when 
  96.     (flags & BDR_OK_FREE) != 0.
  97. */
  98.  
  99. #define DFREE(Dexp) ((flags & BDR_OK_FREE)? Dfree(Dexp): 0)
  100.  
  101.  
  102. class SBinder;  // forward declaration
  103.  
  104.  
  105. /*
  106.     Binder nodes must be all either statically or
  107.     dynamically allocated as indicated by the 
  108.     BDR_OK_FREE flag.  The Binder defaults to 
  109.     BDR_NO_FREE or static meaning its deleting 
  110.     primitives are inhibited and upon Binder destruction
  111.     any remaining nodes are simply left unbound and 
  112.     undeleted.  If you wish to dispose of the nodes then
  113.     be SURE that every node is dynamically allocated and
  114.     that you set the BDR_OK_FREE flag in the constructor
  115.     call.  See Dfree() for details on calling node 
  116.     destructors.
  117. */
  118.  
  119. class Binder {
  120.     friend SBinder;  // SBinder::load, SBinder::store
  121.     unsigned lowLimit, lowThreshold, first;
  122.     voiDV linkS;
  123.     unsigned limit, delta, nodes, maxNodes;
  124.     unsigned curNode, flags;
  125.     BDRcomparE comparE;
  126. protected:
  127.     void construct(unsigned flags, unsigned maxNodes,
  128.         unsigned limit, unsigned delta);
  129.     virtual int Dfree(voiD D) 
  130.         /*
  131.             Dfree() is called by any Binder 
  132.             primitive that attempts to delete
  133.             the data, D, it is unbinding, e.g. 
  134.             atFree(), popFree(), etc.  The 
  135.             Binder must be constructed with
  136.             BDR_OK_FREE for Dfree() to be
  137.             called.  In other words it is 
  138.             guaranteed that Dfree() is never
  139.             called when BDR_OK_FREE is absent.
  140.             If D is NULL then zero must be 
  141.             returned otherwise a nonzero value 
  142.             must be returned regardless of 
  143.             whether or not an overriding 
  144.             function is successful in deleting
  145.             its data.  Override this function to
  146.             implement calling your own node 
  147.             destructors.
  148.         */
  149.         { return (D? delete D, 1 : 0); }
  150.     virtual int Dattach(voiD)
  151.         /*
  152.             Dattach() is called by any Binder
  153.             primitive that attempts to bind 
  154.             data, e.g. atIns(), push(), etc.
  155.             This allows the data to become aware
  156.             of the binding process by overriding
  157.             Dattach().  The voiD parameter is a 
  158.             pointer to the data about to be 
  159.             bound and guaranteed never to be 
  160.             NULL!  If and only if the data can't
  161.             attach itself to the Binder, as 
  162.             defined by the overriding function,
  163.             should zero be returned.  Any 
  164.             overriding function should only
  165.             "link" itself to the Binder but 
  166.             should not use the implicit "this"
  167.             pointer to access Binder within the
  168.             overriding function.  This 
  169.             restriction applies because Binder
  170.             data may be in a transition state
  171.             when Dattach() is called.  It is 
  172.             however permissible to store the 
  173.             "this" pointer within the data being
  174.             bound for later use in accessing 
  175.             Binder data.
  176.         */
  177.         { return 1; }
  178.     virtual void Ddetach(voiD)
  179.         /*
  180.             Ddetach() is called by any Binder
  181.             primitive that attempts to unbind
  182.             data, e.g. atDel(), pop(), etc.
  183.             The voiD parameter is never NULL!
  184.             Once called, the data must consider
  185.             itself detached from the Binder!  
  186.         */
  187.         { return; }
  188. public:
  189.     Binder(UniqueBinderConST) {}
  190.     Binder(unsigned flags = BDR_NO_FREE,
  191.         unsigned maxNodes = BDR_MAXNODES,
  192.         unsigned limit = BDR_LIMIT,
  193.         unsigned delta = BDR_DELTA)
  194.         { construct(flags,maxNodes,limit,delta); }
  195.     Binder(voiDV argv, int argc = 0,
  196.         unsigned flags = BDR_NO_FREE);
  197.     voiDV vector();
  198.     virtual ~Binder();
  199.         /*
  200.             Be sure to override this destructor
  201.             calling allDel() or allFree() in 
  202.             the derived class' destructor to 
  203.             insure that the correct virtual 
  204.             functions are called within allDel()
  205.             and allFree(), i.e. Dfree() and 
  206.             Ddetach()!!!
  207.         */
  208.     unsigned Limit()  { return limit; }
  209.     unsigned setLimit(unsigned newLimit);
  210.     unsigned pack()  { return setLimit(nodes); }
  211.     unsigned Delta()  { return delta; }
  212.     unsigned setDelta(unsigned newDelta = BDR_DELTA);
  213.     unsigned Nodes()  { return nodes; }
  214.     unsigned MaxNodes()  { return maxNodes; }
  215.     unsigned setMaxNodes(unsigned newMaxNodes
  216.         = BDR_MAXNODES);
  217.     unsigned vacancy()  { return maxNodes - nodes; }
  218.     unsigned vacancyNonElastic()
  219.             { return limit - nodes; }
  220.     unsigned Flags(unsigned flag = 
  221.             BDR_SORTED | BDR_OK_FREE)
  222.             { return (flags & flag); }
  223.     voiD atIns(unsigned n, voiD D);
  224.     voiD atDel(unsigned n);
  225.     int  allDel();
  226.     int  atFree(unsigned n)  { return DFREE(atDel(n)); }
  227.     int  allFree();
  228.     voiD atPut(unsigned n, voiD D);
  229.     voiD atGet(unsigned n);
  230.     voiD operator[](unsigned n)  { return atGet(n); }
  231.     voiD atXchg(unsigned n, voiD D);
  232.     unsigned index(const voiD D);
  233.     voiD add(voiD D)  { return atIns(nodes,D); }
  234.     Binder& operator+=(voiD D)
  235.         { atIns(nodes,D); return *this; }
  236.     Binder& operator+(voiD D)
  237.         { atIns(nodes,D); return *this; }
  238.     voiD subtract(voiD D)
  239.         { return atDel(index(D)); }
  240.     Binder& operator-=(voiD D)
  241.         { atDel(index(D)); return *this; }
  242.     Binder& operator-(voiD D)
  243.         { atDel(index(D)); return *this; }
  244.     int  forEach(BDRforEachBlocK B, voiD M = voiD0,
  245.         voiD A = voiD0);
  246.     unsigned firstThat(BDRdetectBlocK B, voiD M = voiD0);
  247.     unsigned lastThat(BDRdetectBlocK B, voiD M = voiD0);
  248.     int  collect(BDRcollectBlocK B, BindeR R,
  249.         voiD M = voiD0, voiD A = voiD0);
  250.  
  251. /*  FlexList like primitives:  */
  252.  
  253.     voiD top()  { return atGet(0); }
  254.     voiD current()  { return atGet(curNode); }
  255.     operator voiD()  { return atGet(curNode); }
  256.     /*
  257.         The implicit (voiD) cast is meant to be
  258.         used in conjunction with the iterators and
  259.         not the << and >> operators!
  260.     */
  261.     voiD bottom()  { return atGet(nodes-1); }
  262.     unsigned CurNode();
  263.     int  setCurNode(unsigned n = BDR_MAXNODES);
  264.     int  Sorted()  { return (flags & BDR_SORTED); }
  265.     void unSort()  { flags &= ~BDR_SORTED; }
  266.     BDRcomparE ComparE()  { return comparE; }
  267.     void setComparE(BDRcomparE comparE)
  268.         { this->comparE = comparE;
  269.             flags &= ~BDR_SORTED; }
  270.     voiD push(voiD D)  { return atIns(0,D); }
  271.     voiD pop()  { return atDel(0); }
  272.     int  popFree()  { return DFREE(atDel(0)); }
  273.     Binder& operator>>(voiD& D)
  274.         { D = atDel(0); return *this; }
  275.     voiD insQ(voiD D)
  276.         { return atIns(nodes,D); }
  277.     Binder& operator<<(voiD D)
  278.         { atIns(nodes,D); return *this; }
  279.     Binder& operator<<(Binder& (*manipulator)
  280.         (Binder& B));
  281.     voiD rmQ()  { return atDel(0); }
  282.     int  rmQFree()  { return DFREE(atDel(0)); }
  283.     voiD unQ() /* Remove from rear of Q */
  284.         { return atDel(nodes-1); }
  285.     int  unQFree()  { return DFREE(atDel(nodes-1)); }
  286.     voiD ins(voiD D);
  287.     voiD insSort(voiD D);
  288.     voiD del();
  289.     int  delFree()  { return DFREE(del()); }
  290.     voiD next();
  291.     voiD operator++()  { return next(); }
  292.     voiD prev();
  293.     voiD operator--()  { return prev(); }
  294.     voiD findFirst(const voiD K);
  295.     voiD findNext (const voiD K);
  296.     voiD findLast (const voiD K);
  297.     voiD findPrev (const voiD K);
  298.     int  sort(BDRcomparE comparE = BDRcomparE0);
  299. };
  300.  
  301.  
  302. #endif
  303.